package coint.btc.dao.auth.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import coint.btc.base.dao.BaseDAO;
import coint.btc.dao.auth.PermissionCustom;
import coint.btc.dto.PermissionDTO;
import coint.btc.pojo.Permission;
import coint.btc.vo.PermissionVo;



@SuppressWarnings("all")
@Repository
public class PermissionJpaImpl extends BaseDAO implements PermissionCustom {

	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public Set<String> findAuthByUserId(Integer id) {
		StringBuilder sql=new StringBuilder(" select * from  permission auth left join juese_permission auth_role on auth.id=auth_role.auth_id ");
		sql.append(" left join juese role on auth_role.juese_id=role.id ");
		sql.append(" left join yonghu admin on role.id=admin.juese_id where admin.user_id=:id");
		return new HashSet<>(getSession().createSQLQuery(sql.toString()).setParameter("id",id,StandardBasicTypes.INTEGER).list());
	}





	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<Permission> findAuthMenuByUserId(Integer userId,Integer typeId) {
		StringBuilder sql=new StringBuilder("select auth.id,auth.name,auth.mgr_id,auth.biaoshi,auth.qx_order,auth.type,auth.qx_url from yonghu admin  ");
		sql.append(" left join  juese role on admin.juese_id=role.id ");
		sql.append(" left join juese_permission roleAuth on role.id=roleAuth.juese_id ");
		sql.append(" left join permission auth on roleAuth.quanxian_id=auth.id ");
	    sql.append("  where admin.user_id="+userId+" and auth.type in (1,2) and role.type_id="+typeId+" order by auth.qx_order asc");
		return getSession().createSQLQuery(sql.toString()).addEntity(Permission.class).list();
	}



	@Override
	@Transactional( rollbackFor = Exception.class)
	public int addQuanXian(Permission quanxian) {
		
		try {
			getSession().save(quanxian);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return  0 ;
		}
		
	}

	@Override
	public boolean CheckDouble(String name) {
		
			String sql  = "SELECT count(name) as counts FROM Permission where  name = '"+name+"'";
			Long count = (Long)getSession().createQuery(sql).uniqueResult();  
			 if(count>=1) {
				 return false;
			 }else {
				 
				 return true;
			 }
		
	}



	@Override
	public int updateDaozhaQuanxian(Permission quanxian) {
		try {
			getSession().merge(quanxian);
			return 1;
		} catch (Exception e) {
			return  0 ;
		}
	}





	@Override
	public List<Permission> loadQuanXianTree() {
		StringBuilder sql=new StringBuilder("SELECT auth.id,auth. NAME,auth.mgr_id,auth.biaoshi,auth.qx_order,auth.type,auth.qx_url FROM  yonghu admin ");
		sql.append(" LEFT JOIN juese roles on admin.juese_id = roles.id ");
		sql.append(" LEFT JOIN juese_permission roleAuth  ON  roles.id = roleAuth.juese_id ");
		sql.append(" LEFT JOIN permission auth ON roleAuth.permission_id = auth.id ");
	    sql.append("  where   auth.type in (1,2)  order by auth.qx_order asc");
		return getSession().createSQLQuery(sql.toString()).addEntity(Permission.class).list();
	}





	@Override
	public int deletePermission(Integer id) {
		
			String hql="delete from Permission lib where lib.id=" + id;
			Query queryupdate=getSession().createQuery(hql);
			int ret=queryupdate.executeUpdate();
			
			return ret;
		
	}





	@Override
	public coint.btc.vo.Page<PermissionVo> Page(PermissionDTO p, coint.btc.vo.Page<PermissionVo> parms) {
		String sql = "select sql_calc_found_rows id,name,mgr_id,biaoshi,qx_order,type,qx_url  from permission where 1=1";
		if(StringUtils.isNotBlank(p.getName())) sql += " and auth_name like '%"+p.getName()+"%'";
		if(p.getType() != null) sql += " and type ="+p.getType();
		sql += " order by id desc";
		List<Permission> list = getSession().createSQLQuery(sql).addEntity(Permission.class).setFirstResult(parms.getRows() * (parms.getPage() - 1)).setMaxResults(parms.getRows()).list();
		List<PermissionVo>  volist = new ArrayList<PermissionVo>();
		for(int i = 0 ; i<list.size();i++) {
			PermissionVo  temp = new PermissionVo();
			BeanUtils.copyProperties(list.get(i), temp);
			volist.add(temp);
		}
		
		Object uniqueResult = getSession().createSQLQuery("select found_rows()").uniqueResult();
		parms.setTotal(uniqueResult);
		parms.setResults(volist);
		return parms;
	}







	






	
	
}
